package com.interview.communication;

import org.junit.Test;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static org.junit.Assert.assertEquals;

/**
 * Lock + Condition 方式的单元测试
 */
public class LockConditionExampleTest {

    @Test
    public void testLockConditionDemo() throws InterruptedException {
        System.out.println("===== Lock + Condition 测试开始 =====");
        long startTime = System.currentTimeMillis();
        
        LockConditionExample.demo();
        
        long endTime = System.currentTimeMillis();
        System.out.println("===== Lock + Condition 测试完成，耗时: " + (endTime - startTime) + "ms =====\n");
    }

    @Test
    public void testLockConditionBasic() throws InterruptedException {
        // 验证Lock和Condition的基本操作
        System.out.println("===== Lock + Condition 基本操作测试 =====");
        
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        final int[] value = {0};
        
        // 生产线程
        Thread producer = new Thread(() -> {
            lock.lock();
            try {
                value[0] = 42;
                System.out.println("生产者设置值: " + value[0]);
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        });
        
        // 消费线程
        Thread consumer = new Thread(() -> {
            lock.lock();
            try {
                while (value[0] == 0) {
                    condition.await();
                }
                System.out.println("消费者获取值: " + value[0]);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        });
        
        consumer.start();
        Thread.sleep(100);  // 确保消费者先等待
        producer.start();
        
        producer.join();
        consumer.join();
        
        assertEquals("值不正确", 42, value[0]);
        System.out.println("✓ Lock + Condition 基本操作验证通过\n");
    }

    @Test
    public void testMultipleConditions() throws InterruptedException {
        // 验证多个Condition的使用
        System.out.println("===== 多个Condition测试 =====");
        
        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        
        final boolean[] signal1 = {false};
        final boolean[] signal2 = {false};
        
        // 线程1：等待condition1
        Thread thread1 = new Thread(() -> {
            lock.lock();
            try {
                while (!signal1[0]) {
                    condition1.await();
                }
                System.out.println("线程1 收到信号1");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        });
        
        // 线程2：等待condition2
        Thread thread2 = new Thread(() -> {
            lock.lock();
            try {
                while (!signal2[0]) {
                    condition2.await();
                }
                System.out.println("线程2 收到信号2");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        });
        
        thread1.start();
        thread2.start();
        
        Thread.sleep(100);
        
        // 发送信号1
        lock.lock();
        try {
            signal1[0] = true;
            condition1.signalAll();
            System.out.println("发送信号给线程1");
        } finally {
            lock.unlock();
        }
        
        Thread.sleep(100);
        
        // 发送信号2
        lock.lock();
        try {
            signal2[0] = true;
            condition2.signalAll();
            System.out.println("发送信号给线程2");
        } finally {
            lock.unlock();
        }
        
        thread1.join();
        thread2.join();
        
        System.out.println("✓ 多个Condition验证通过\n");
    }
}

